package com.example.ordersys.controller;

import com.example.ordersys.config.AppFinal;
import com.example.ordersys.mapper.UserMapper;
import com.example.ordersys.model.UserInfo;
import com.example.ordersys.tools.ResponseBody;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Resource
    private UserMapper userMapper;
    //模板
    @RequestMapping("/welcome")
    public ResponseBody<String> welcome(HttpServletRequest httpServletRequest){
        int status=-1;
        String message="请先登录";
        String data=null;
        return new ResponseBody<>(status,message,data);
    }

    //查询用列表
    @RequestMapping("/getUserList")
    public ResponseBody<List<UserInfo>> getUserList(){
        int status=-1;
        String message="获取用户列表失败";
        List<UserInfo> data;
        data=userMapper.getUserList();
        if (data!=null){
            status=0;
            message="";
        }
        return new ResponseBody<>(status,message,data);
    }
    /**
     * 登录功能
     */
    @RequestMapping("/login")
    public ResponseBody<UserInfo> login(UserInfo userinfo,
                                        HttpServletRequest request){
        int status=-1;
        String message="登陆失败";
        UserInfo data=null;
        data = userMapper.login(userinfo);
        if (data==null){
            userinfo.setIsadmin(0);
            if (userMapper.login(userinfo)!=null)
                message="管理员登录，权限不足";
        }
        if (data != null) {
            // 登录成功，设置
            HttpSession session = request.getSession();
            // 存放到 Session
            session.setAttribute(AppFinal.USERINFO_SESSIONKEY, data);
            status=0;
            message="";

        }
        return new ResponseBody<>(status,message,data);
    }

    /**
     * 登录功能
     */
    @RequestMapping("/reg")
    public ResponseBody<Integer> register(UserInfo user) {
        int status=-1;
        String message="注册失败";
       Integer data=0;
       data=userMapper.register(user);
       if (data>0){
           status=0;
           message="注册成功";
       }
        return new ResponseBody<>(status,message,data);

    }

    /**
     * 退出登录
     */
    @RequestMapping("/logout")
    public ResponseBody<Integer> logout(HttpServletRequest request) {
       if (AppFinal.getUserBySession(request)!=null){
           request.getSession(false).removeAttribute(AppFinal.USERINFO_SESSIONKEY);
       }
        return new ResponseBody<>(0,"",null);
    }

    /**
     * 判断用户是否已经登录
     */
    @RequestMapping("/islogin")
    public ResponseBody<UserInfo> isLogin(HttpServletRequest request) {
        UserInfo data =null;
        int status=-1;
        String message="请先登录";
        if ((data=AppFinal.getUserBySession(request))!=null){
            status=0;
            message="";
        }
        return new ResponseBody<>(status,message,data);
    }
}
